package com.craftyworks.alejandro.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpHost;
import org.apache.http.HttpMessage;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

public class HttpClientTemplate {

    private final static int DEFAULT_TIMEOUT = 1000;
    
    private final static String DEFAULT_ENCODING = "UTF-8";
    
    private int timeout = DEFAULT_TIMEOUT;
    
    private HttpHost proxy;
    
    private String encoding = DEFAULT_ENCODING;
    
    private Map<String, String> defaultHeader; 

    public HttpClientTemplate() {
        if(defaultHeader == null) {
            defaultHeader = new HashMap<String, String>();
            defaultHeader.put("Content-Type", "application/x-www-form-urlencoded");
        }
    }
    
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public void setProxy(HttpHost proxy) {
        this.proxy = proxy;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public void setDefaultHeader(Map<String, String> defaultHeader) {
        this.defaultHeader = defaultHeader;
    }

    public HttpResponse getHttpResponse(String url) throws IOException {
        return getHttpResponse(url, null);
    }
    
    public HttpResponse getHttpResponse(String url, Map<String, String> header) throws IOException {
        HttpGet httpRequest = new HttpGet(url);
        setRequestHeader(httpRequest, header);
        return executeHttpRequest(httpRequest);
    }
    
    public HttpResponse sendHttpRequest(String url, String body) throws IOException {
        return sendHttpRequest(url, null, body);
    }
    
    public  HttpResponse sendHttpRequest(String url, Map<String, String> header, String body) throws IOException {
        HttpPost httpRequest = new HttpPost(url);
        setRequestHeader(httpRequest, header);
        httpRequest.setEntity(new StringEntity(body, encoding));
        return executeHttpRequest(httpRequest);
    }

    private HttpResponse executeHttpRequest(HttpUriRequest httpRequest) throws IOException, ClientProtocolException {
        HttpClient httpClient = new DefaultHttpClient();
        if(proxy != null) {
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); 
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
        return httpClient.execute(httpRequest);
    }

    private void setRequestHeader(HttpMessage httpMessage, Map<String, String> header) {
        if(defaultHeader != null) {
            for(Entry<String, String> entry : defaultHeader.entrySet()) {
                httpMessage.setHeader(entry.getKey(), entry.getValue());
            }
        }
        if(header != null) {
            for(Entry<String, String> entry : header.entrySet()) {
                httpMessage.setHeader(entry.getKey(), entry.getValue());
            }
        }
    }

}
